<?php

namespace app\admin\controller\files;

use app\admin\controller\Controller;
use app\admin\model\article\Article as ArticleModel;
use app\admin\model\article\Category as CategoryModel;
use think\Session;
use think\Db;

/**
 * 房源控制器模型
 * Class Category
 * @package app\store\controller\content
 */
class Build extends Controller{
    public function index(){
        $userinfo=Db::name('love_admin_user')->where(['id'=>Session::get('yoshop_admin')['user']['id']])->find();
        $ser=input('get.search');
        if($ser){
            $where['name']=["like",'%'.trim($ser).'%'];
        }
        if($userinfo['type']==1){
            //主管理员
            $where['id']=['>',0];
        }elseif ($userinfo['type']==2){
            //城市管理员
            $where['cityId']=['=',$userinfo['cityid']];
        }else{
            //区域管理员
            $where['areaId']=['=',$userinfo['areaid']];
        }
        $list=Db::name('love_building')->where($where)->order('createTime desc')->paginate(15, false, [
            'query' => request()->request()
        ]);
        return $this->fetch('index', compact('list'));
    }
    /**
     * 下架房源
     */
    public function delete($id){
        //房源详情
        $update=Db::name('love_building')->where(['id'=>$id])->update(['status'=>2]);
        if (!$update) {
            return $this->renderError('下架失败');
        }
        return $this->success('下架成功','index');
    }

    /**
     * 上架房源
     */
    public function up($id){
        $update=Db::name('love_building')->where(['id'=>$id])->update(['status'=>1]);
        if (!$update) {
            return $this->renderError('上架失败');
        }
        return $this->success('上架成功','index');
    }
    /**
     * 页面预览
     */
    public function info($id){
        $buildId=$id;
        if(empty($buildId)){
            return json(['msg'=>'楼盘id不能为空','code'=>'error','data'=>[]]);
        }
        //判断该房源状态
        $buildInfo=Db::name('love_building')->where(['id'=>$buildId])->find();
        if(empty($buildInfo)){
            return json(['msg'=>'未获取到楼盘信息','code'=>'error','data'=>[]]);
        }
        if($buildInfo['status']==2){
            return json(['msg'=>'该楼盘已下架','code'=>'error','data'=>[]]);
        }
        //获取首页轮播，楼盘轮播，沙盘轮播，配套图片，售楼处图片
        $buildInfo['bannerids']=$this->getImageUrl(json_decode($buildInfo['bannerids'],'true'));
        $buildInfo['loupanimageids']=$this->getImageUrl(json_decode($buildInfo['loupanimageids'],'true'));
        $buildInfo['shapanimageids']=$this->getImageUrl(json_decode($buildInfo['shapanimageids'],'true'));
        $buildInfo['peitaoimageids']=$this->getImageUrl(json_decode($buildInfo['peitaoimageids'],'true'));
        $buildInfo['slimageids']=$this->getImageUrl(json_decode($buildInfo['slimageids'],'true'));
        $buildInfo['characteristic']=explode('@',$buildInfo['characteristic']);
        $buildInfo['qita']=explode('@',$buildInfo['qita']);
        $this->view->engine->layout(false);
        return $this->fetch('info', compact('buildInfo'));
    }


    /**
     * 获取图片链接
     */
    private function getImageUrl($ids){
        $imageInfos=Db::name('upload_file')->where('file_id','in',$ids)->select();
        $arr=[];
        if($imageInfos){
            foreach($imageInfos as $k=>$v){
                $arr[]=$v['file_url'].'/'.$v['file_name'];
            }
        }
        return $arr;
    }
    /**
     * 添加房源
     */
    public function add(){
        if(empty($this->postData('goods')['name'])){
            //获取城市区域信息
            $roleList=Db::name('love_area')->where(['pid'=>0,'status'=>1])->field('id,name,pid')->select()->toArray();
            //获取城市,区域,楼盘三级联动
            $newArray=[];
            foreach ($roleList as $k=>$v){
                $city=Db::name('love_area')->where(['pid'=>$v['id'],'status'=>1])->field('id,name,pid')->select()->toArray();
                $newArray[$v['id']]=$v;
                $newArray[$v['id']]['level']=1;
                foreach ($city as $k1=>$v1){
                    $newArray[$v['id']]['city'][$v1['id']]=$v1;
                    $newArray[$v['id']]['city'][$v1['id']]['level']=2;
                    $newArray[$v['id']]['city'][$v1['id']]['region']=[];
                    $region=Db::name('love_building')->where(['cityId'=>$v1['id'],'status'=>1])->field('id,name')->select()->toArray();
                    foreach ($region as $k2=>$v2){
                        $newArray[$v['id']]['city'][$v1['id']]['region'][$v2['id']]=$v2;
                        $newArray[$v['id']]['city'][$v1['id']]['region'][$v2['id']]['pid']=$v1['id'];
                        $newArray[$v['id']]['city'][$v1['id']]['region'][$v2['id']]['level']=3;
                    }
                }
            }
            return $this->fetch('add',compact('newArray'));
        }
        //新增房源
        $data=$this->postData('goods');
        $data['jingdu']=explode(',',$data['jingdus'])[0];
        $data['weidu']=explode(',',$data['jingdus'])[1];
        unset($data['jingdus']);
        $data['loupanimageids']=json_encode($data['loupanimageids']);
        $data['shapanimageids']=json_encode($data['shapanimageids']);
        $data['peitaoimageids']=json_encode($data['peitaoimageids']);
        $data['slimageids']=json_encode($data['slimageids']);
        $data['bannerids']=json_encode($data['bannerids']);
        $data['createTime']=time();
        $data['companyId']=Session::get('yoshop_admin')['user']['id'];
        $insert=Db::name('love_building')->insertGetId($data);
        if($insert){
            return $this->success('添加成功', 'index');
        }
        return $this->renderError('添加失败');
    }

    /**
     * 更新房源信息
     */
    public function edit($id){
        //房源详情
        //$model = ArticleModel::detail($id);
        $model = Db::name('love_building')->where(['id'=>$id])->find();
        $bannerids=json_decode($model['bannerids'],true);
        $shapanimageids=json_decode($model['shapanimageids'],true);
        $peitaoimageids=json_decode($model['peitaoimageids'],true);
        $slimageids=json_decode($model['slimageids'],true);
        $loupanimageids=json_decode($model['loupanimageids'],true);
        $model['bannerids']=Db::name('upload_file')->where('file_id','in',$bannerids)->select();
        $model['shapanimageids']=Db::name('upload_file')->where('file_id','in',$shapanimageids)->select();
        $model['peitaoimageids']=Db::name('upload_file')->where('file_id','in',$peitaoimageids)->select();
        $model['slimageids']=Db::name('upload_file')->where('file_id','in',$slimageids)->select();
        $model['loupanimageids']=Db::name('upload_file')->where('file_id','in',$loupanimageids)->select();
        if (empty($this->postData('goods')['name'])){
            //获取城市区域信息
            $roleList=Db::name('love_area')->where(['pid'=>0])->field('id,name,pid')->select()->toArray();
            //获取城市,区域,楼盘三级联动
            $newArray=[];
            foreach ($roleList as $k=>$v){
                $city=Db::name('love_area')->where(['pid'=>$v['id']])->field('id,name,pid')->select()->toArray();
                $newArray[$v['id']]=$v;
                $newArray[$v['id']]['level']=1;
                foreach ($city as $k1=>$v1){
                    $newArray[$v['id']]['city'][$v1['id']]=$v1;
                    $newArray[$v['id']]['city'][$v1['id']]['level']=2;
                    $newArray[$v['id']]['city'][$v1['id']]['region']=[];
                    $region=Db::name('love_building')->where(['cityId'=>$v1['id']])->field('id,name')->select()->toArray();
                    foreach ($region as $k2=>$v2){
                        $newArray[$v['id']]['city'][$v1['id']]['region'][$v2['id']]=$v2;
                        $newArray[$v['id']]['city'][$v1['id']]['region'][$v2['id']]['pid']=$v1['id'];
                        $newArray[$v['id']]['city'][$v1['id']]['region'][$v2['id']]['level']=3;
                    }
                }
            }
            return $this->fetch('edit', compact('model','newArray'));
        }
        //更新房源信息
        $data=$this->postData('goods');
        $data['jingdu']=explode(',',$data['jingdus'])[0];
        $data['weidu']=explode(',',$data['jingdus'])[1];
        unset($data['jingdus']);
        $data['loupanimageids']=json_encode($data['loupanimageids']);
        $data['shapanimageids']=json_encode($data['shapanimageids']);
        $data['peitaoimageids']=json_encode($data['peitaoimageids']);
        $data['slimageids']=json_encode($data['slimageids']);
        $data['bannerids']=json_encode($data['bannerids']);
        $data['updateTime']=time();
        $insert=Db::name('love_building')->where(['id'=>$id])->update($data);
        if ($insert) {
            return $this->success('修改成功', 'files.build/index');
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }
}
